﻿<template>
  <div class="app-container">
    <div class="chart-title with-back">
      <div class="chart-left">
        <div>终端采购单</div>
        <div class="backtrack">
          <el-button @click="comeBack">返回</el-button>
        </div>
      </div>
    </div>
    <div class="chart-content">
      <div class="filter-title">
        <div></div>
        <div class="filter-right">
          <div
            class="add-button"
            @click="handleCreate"
          >
            <img src="@/assets/images/icon_add@2x.png" />
            <span>新建</span>
          </div>
        </div>
      </div>
      <div class="chart-list">
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(255,255,255,0.2)"
          border
          fit
          highlight-current-row
          style="width: 100%min-height:1000px"
        >
          <el-table-column
            fixed="left"
            align="center"
            :label="$t('table.id')"
            width="65"
          >
            <template slot-scope="scope">
              <span>{{
                (listQuery.page - 1) * listQuery.limit + scope.$index + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('track.code')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.track_id }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('goods.name_1')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.goods_name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('goods.pictrue')"
          >
            <template slot-scope="scope">
              <el-image
                v-if="scope.row.goods_img"
                style="width: 100px; height: 100px"
                :src="scope.row.goods_img"
                :fit="fit"
              ></el-image>
              <span v-else> 暂无图片 </span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('goods.weight_1')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.goods_weight }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('track.sensor')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.sensor_capacity }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('terminal.sumNumber')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.track_capacity }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            :label="$t('stock.goodsStock')"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.goods_stock }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('table.actions')"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="operat-button">
                <div
                  class="read-img"
                  @click="handleRead(scope.row)"
                >
                  <img src="@/assets/images/read@2x.png" />
                </div>
                <div
                  class="read-img"
                  @click="handleModifyStatus(scope.row, 'deleted')"
                >
                  <img src="@/assets/images/delete@2x.png" />
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
      <!-- 编辑 -->
      <el-dialog
        :title="textMap[dialogStatus]"
        :visible.sync="dialogFormVisible"
      >
        <el-form
          ref="dataForm"
          :rules="rules"
          :model="temp"
          label-position="right"
          label-width="35%"
          style="width: 400px margin-left:120px"
          size="small"
        >
          <div class="pops">
            <div class="information-title">
              <span class="icon-title"></span>
              <span>终端信息:</span>
            </div>
            <div class="pops-item">
              <div class="pops-left">
                <el-form-item
                  :label="$t('track.code') + ' :'"
                  prop="track_id"
                >
                  <el-input-number
                    class="left-number"
                    v-model="temp.track_id"
                    :controls="false"
                    :disabled="dialogStatus === 'read' ? true : false"
                  ></el-input-number>
                </el-form-item>
                <el-form-item
                  :label="$t('track.sensor') + ' :'"
                  prop="sensor_capacity"
                >
                  <el-input-number
                    class="left-number"
                    v-model="temp.sensor_capacity"
                    :controls="false"
                    :disabled="dialogStatus === 'read' ? true : false"
                  ></el-input-number>
                </el-form-item>
              </div>
              <div class="pops-right">
                <el-form-item
                  :label="$t('terminal.sumNumber') + ' :'"
                  prop="track_capacity"
                >
                  <el-input-number
                    class="left-number"
                    v-model="temp.track_capacity"
                    :controls="false"
                    :disabled="dialogStatus === 'read' ? true : false"
                  ></el-input-number>
                </el-form-item>
              </div>
            </div>
          </div>
        </el-form>
        <div
          slot="footer"
          class="dialog-footer between-space"
        >
          <div>
            <el-button
              v-show="dialogStatus == 'read'"
              type="primary"
              @click="handleEdit"
            >{{ $t("table.edit") }}</el-button>
          </div>
          <div>
            <el-button @click="dialogFormVisible = false">{{
              $t("table.cancel")
            }}</el-button>
            <el-button
              v-if="dialogStatus == 'create'"
              type="primary"
              :disabled="!createDisabled"
              @click="createData"
            >{{ $t("table.confirm") }}</el-button>
            <el-button
              v-else-if="dialogStatus == 'edit'"
              type="primary"
              :disabled="!updateDisabled"
              @click="updateData"
            >{{ $t("table.confirm") }}</el-button>
            <el-button
              v-else
              type="primary"
              @click="dialogFormVisible = false"
            >{{ $t("table.confirm") }}</el-button>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script type="text/javascript">
import { getToken } from "@/utils/auth";
export default {
  name: "CargoLaneManag",
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      id: undefined,
      temp: {
        id: undefined,
        track_id: undefined,
        sensor_capacity: undefined,
        track_capacity: undefined
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        edit: "编辑",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      rules: {
        track_id: [{ required: true, message: "请填写货道编号", trigger: "blur" }],
        sensor_capacity: [{ required: true, message: "请填写传感器容量", trigger: "blur" }],
        track_capacity: [{ required: true, message: "请填写货道容量", trigger: "blur" }]
      },
      createDisabled: true,
      updateDisabled: true
    };
  },
  created() {
    const token = getToken()
    if (token && !this.$store.state.socket.loginTransition && !this.$store.state.socket.loginAccount) {
      this.tokenLogin(token)
    }
    if (this.$route.query.id) {
      this.id = this.$route.query.id
    }
    this.getList();
  },
  methods: {
    tokenLogin(token) {
      const path = {
        app: "wxadmin",
        api: "api_wxadmin_admin_keepLogin",
        version: "1.0",
        type: 2,
        data: {
          token: token
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        this.socketData(res);
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      const path = {
        app: "user",
        api: "api_equipment_track_getList",
        version: "1.0",
        type: 2,
        data: {
          equipment_id: this.id ? this.id : ''
        }
      }
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleCreate() {
      this.dialogStatus = "create";
      this.resetTemp()
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleEdit() {
      this.dialogStatus = "edit";
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        track_id: undefined,
        sensor_capacity: undefined,
        track_capacity: undefined
      };
    },
    // 更新数据
    upData() {
      const data = {
        id: this.temp.id ? this.temp.id : "",
        equipment_id: this.id ? this.id : "",
        track_id: this.temp.track_id ? this.temp.track_id : "",
        sensor_capacity: this.temp.sensor_capacity ? this.temp.sensor_capacity : "",
        track_capacity: this.temp.track_capacity ? this.temp.track_capacity : ""
      };
      return data;
    },
    createData() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.createDisabled) {
            this.createDisabled = false;
            const data = this.upData();
            const path = {
              app: "equipment",
              api: "api_equipment_track_add",
              version: "1.0",
              type: 2,
              data: data
            };
            this.socketApi.sendSock(JSON.stringify(path), res => {
              if (res) {
                this.socketData(res);
              }
              setTimeout(() => {
                this.createDisabled = true;
              }, 1000);
            });
          }
        }
      });
    },
    // 更新货道档案
    updateData() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.updateDisabled) {
            this.updateDisabled = false;
            const data = this.upData();
            const path = {
              app: "equipment",
              api: "api_equipment_track_edit",
              version: "1.0",
              type: 2,
              data: data
            };
            this.socketApi.sendSock(JSON.stringify(path), res => {
              if (res) {
                this.socketData(res);
              }
              setTimeout(() => {
                this.updateDisabled = true;
              }, 1000);
            });
          }
        }
      });
    },
    handleModifyStatus(row) {
      this.$confirm("此操作将永久删除该货道信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const path = {
            app: "equipment",
            api: "api_equipment_track_delete",
            version: "1.0",
            type: 2,
            data: {
              id: row.id
            }
          };
          this.socketApi.sendSock(JSON.stringify(path), res => {
            if (res) {
              this.socketData(res);
            } else {
              this.$message.error(" 删除失败 ");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "api_wxadmin_admin_keepLogin") {
            this.$store.dispatch("loginTransition", true);
            this.socketApi.sendCache(res => {
              this.socketData(res);
            });
          } else if (resj.api === "api_equipment_track_getList") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          } else if (resj.api === "api_equipment_track_add") {
            this.$notify({
              title: "成功",
              message: "添加成功",
              type: "success",
              duration: 2000
            });
            this.dialogFormVisible = false;
            this.getList();
          } else if (resj.api === "api_equipment_track_edit") {
            this.$notify({
              title: "成功",
              message: "更新成功",
              type: "success",
              duration: 2000
            });
            this.dialogFormVisible = false;
            this.getList();
          } else if (resj.api === "api_equipment_track_delete") {
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success",
              duration: 2000
            });
            this.dialogFormVisible = false;
            this.getList();
          }
        }
      }
    },
    comeBack() {
      this.$router.push("/terminal-data/terminal-list");
    }
  }
};
</script>
